#include"bootpack.h"


//初始化显示屏
void init_screen (unsigned char * screen_buf  ,int xsize ,int ysize) {
			
	int taskbar_high  = 50 ;
	int btn_width = 70;
	
	 //btinfo->vram;//显存缓冲的起始地址  一直到0xa0000 - 0xaffff 
	boxfill8 (screen_buf,xsize ,COL8_840084,0,0 ,xsize-1,ysize-taskbar_high+1) ;
	boxfill8 (screen_buf,xsize ,COL8_C6C6C6 ,0,ysize-taskbar_high ,xsize-1,ysize-taskbar_high) ;
	boxfill8 (screen_buf,xsize ,COL8_FFFFFF ,0,ysize-taskbar_high+1 ,xsize-1,ysize-taskbar_high+1) ;
	boxfill8 (screen_buf,xsize ,COL8_C6C6C6 ,0,ysize-taskbar_high+2 ,xsize-1,ysize-1) ;

	boxfill8(screen_buf, xsize, COL8_FFFFFF,  3,    ysize - taskbar_high+4, btn_width -1,     ysize - taskbar_high+4);
	boxfill8(screen_buf, xsize, COL8_FFFFFF,  2,     ysize - taskbar_high+4,  4,     ysize -  4);
	boxfill8(screen_buf, xsize, COL8_848484,  3,     ysize -  4, btn_width-1,     ysize -  4);
	boxfill8(screen_buf, xsize, COL8_848484, btn_width-1,    ysize - taskbar_high+5, btn_width-1+2,     ysize -  5);
	putchars8 (screen_buf ,xsize ,12,ysize -taskbar_high/2 -16,COL8_000000 ,"start") ;
}

//画图写字
void putchars8_sht (SHEET * sht ,int x ,int y  ,int color ,int backcolor ,char * s ,int length) { 
	 SHEET_MANAGEMENT * shtman2  = sht->shtman ;
	boxfill8 (sht->buf,sht->bxsize,backcolor,x ,y ,x+length*8-1,y+15);
	putchars8(sht->buf,sht->bxsize,x,y,color,s);
	sheet_refresh_bysht2(sht,x,y,x +length*8-1 ,y+15) ;

	//void sheet_refresh_between (SHEET_MANAGEMENT * shtman ,int vx0 ,int vy0 ,int vx1 ,int vy1 ,int h0 ,int h1)
} 

//输出字符串
void putchars8 (char *vram ,int xsize ,int x ,int y ,int color ,char *str){ 
	extern char hankaku[4096] ;
	for (; *str !='\0';str++){ 
		putchar8(vram ,xsize,x,y,color,hankaku+(*str)*16);
		x+=8; //因为字体是8*16像素的 ，下一个字符要向右走8个像素
	}
}
void putchar8 (char * vram ,int xsize ,int x , int y,int color ,char *font ) { 
	/*static char A[16] = { 
		0x00,0x18,0x18,0x18,0x18,0x24,0x24,0x24,
		0x24,0x7e,0x42,0x42,0x42,0x24,0x00,0x00
	} ;
	*/
	char x8[] = {0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01} ; // 10000000 到00000001
	int i , j ;
	for (i = 0 ;i <0x10 ;i++){   //竖着16个字节
		for (j = 0 ; j<0x08;j++) {//横着8个字节
			if (x8[j]&font[i])	{
				vram [(y+i)*xsize +x+j] = color ;
			}
		}
	}

}

/*填充一个矩形*/
void boxfill8 (unsigned char *vram ,int xsize ,unsigned char color ,int x0 ,int y0 ,int x1 ,int y1) { 
	int x, y ; 
	for (y= y0 ; y<=y1 ;y++){
		for (x= x0 ; x<=x1 ;x++){ 
			vram [y*xsize +x] = color ;
		}
	}
	return ;
}








void init_palette(void){
	int b ,r ,g ;  
	static unsigned char table_rgb[16 * 3] = {
		0x00, 0x00, 0x00,	/*  0:黑 */
		0xff, 0x00, 0x00,	/*  1:亮红 */
		0x00, 0xff, 0x00,	/*  2:亮绿*/
		0xff, 0xff, 0x00,	/*  3:亮黄*/
		0x00, 0x00, 0xff,	/*  4:亮蓝*/
		0xff, 0x00, 0xff,	/*  5:亮紫*/
		0x00, 0xff, 0xff,	/*  6:浅亮蓝*/
		0xff, 0xff, 0xff,	/*  7:白*/
		0xc6, 0xc6, 0xc6,	/*  8:亮灰*/
		0x84, 0x00, 0x00,	/*  9:暗红*/
		0x00, 0x84, 0x00,	/* 10:暗绿 */
		0x84, 0x84, 0x00,	/* 11:暗黄*/
		0x00, 0x00, 0x84,	/* 12:暗青*/
		0x84, 0x00, 0x84,	/* 13:暗紫*/
		0x00, 0x84, 0x84,	/* 14:浅暗蓝*/
		0x84, 0x84, 0x84	/* 15:暗灰*/
	};
	unsigned char table2[216*3];

	set_palette(0, 15, table_rgb);

	for (b= 0 ;  b < 6 ;b++) { 
		for (g = 0 ; g < 6 ; g++) { 
			for (r =0 ; r < 6 ;r++) { 
				table2[(r+g*6+b*36)*3+0] = r*51 ;
				table2[(r+g*6+b*36)*3+1] = g*51 ;
				table2[(r+g*6+b*36)*3+2] = b*51 ;
			}
		}
	}
	set_palette(16, 231, table2);
	return;
}

unsigned char  rgb2pal (int r,int g,int b ,int x ,int y ) { 
	static int table[4] = {3,1,0,2};
	int i ; 

	x &=1 ;//判断奇偶
	y &=1 ;
	i = table[x+y*2] ; /*用于生成色差*/
	r = (r*21) /256 ; //
	g = (g*21) /256 ;
	b = (b*21) /256 ;
	r= (r+i)/4 ;
	g= (g+i)/4 ;
	b= (b+i)/4 ;
	return 16 +r +g*6 +b*36 ;
}

void set_palette(int start, int end, unsigned char *rgb ){ 
	int i ,eflags ; 
	eflags = io_load_eflags ();  /*记录中断许可标志  （实际上是保存FR寄存器所有的位状态）*/
	io_cli(); //禁止中断
	io_out8(0x03c8,start);
	for (i = start; i <= end; i++) {
		io_out8(0x03c9, rgb[0] / 4);
		io_out8(0x03c9, rgb[1] / 4);
		io_out8(0x03c9, rgb[2] / 4);
		rgb += 3;
	}
	io_store_eflags(eflags);	/* 复原中断许可标志 */
	return;


}


void init_mouse_cursor8(char *mouse, char bc)
/* ƒ}ƒEƒXƒJ[ƒ\ƒ‹‚ð€”õi16x16j */
{
	static char cursor[16][16] = {
		"**************..",
		"*OOOOOOOOOOO*...",
		"*OOOOOOOOOO*....",
		"*OOOOOOOOO*.....",
		"*OOOOOOOO*......",
		"*OOOOOOO*.......",
		"*OOOOOOO*.......",
		"*OOOOOOOO*......",
		"*OOOO**OOO*.....",
		"*OOO*..*OOO*....",
		"*OO*....*OOO*...",
		"*O*......*OOO*..",
		"**........*OOO*.",
		"*..........*OOO*",
		"............*OO*",
		".............***"
	};
	int x, y;
	for (y = 0 ; y<16;y++){ 
		for (x= 0 ; x<16 ;x++){ 
			if (cursor[y][x]=='*'){ 
				mouse[y*16 +x] = COL8_000000 ; //黑色
			}
			if (cursor[y][x]=='O'){ 
				mouse[y*16 +x] = COL8_FFFFFF ; //白色
			}
			if (cursor[y][x] == '.') {
				mouse[y * 16 + x] = bc;  //传入的背景色
			}
		}
	}	
	return;
}
/**
	vram : 显存
	screen_x : 屏幕分辨率x
	picsize_x : 图片的x分辨率
	picsize_y : 图片的y分辨率
	x0: 屏幕的x 位置(画图点)
	y0: 屏幕的y 位置(画图点)
	char * buff: 图片
	
*/
void putblock8(char * vram ,int screen_x ,int picsize_x ,int picsize_y ,int x0 ,int y0 ,char *buf ){    //画图 
	int x,y ;
	for (y =0 ; y<picsize_y;y++){ 
		for (x=0 ;x<picsize_x ;x++){ 
			vram[ (y0+y)*screen_x +x0+x] = buf[y*picsize_x + x] ;
		}
	}
}
// act 表示是否为活跃窗口
void make_window8(unsigned char *buf, int xsize, int ysize, char *title ,char act)
{
	
	boxfill8(buf, xsize, COL8_C6C6C6, 0,         0,         xsize - 1, 0        );
	boxfill8(buf, xsize, COL8_FFFFFF, 1,         1,         xsize - 2, 1        );
	boxfill8(buf, xsize, COL8_C6C6C6, 0,         0,         0,         ysize - 1);
	boxfill8(buf, xsize, COL8_FFFFFF, 1,         1,         1,         ysize - 2);
	boxfill8(buf, xsize, COL8_848484, xsize - 2, 1,         xsize - 2, ysize - 2);
	boxfill8(buf, xsize, COL8_000000, xsize - 1, 0,         xsize - 1, ysize - 1);
	boxfill8(buf, xsize, COL8_C6C6C6, 2,         2,         xsize - 3, ysize - 3);
	boxfill8(buf, xsize, COL8_848484, 1,         ysize - 2, xsize - 2, ysize - 2);
	boxfill8(buf, xsize, COL8_000000, 0,         ysize - 1, xsize - 1, ysize - 1);
	
	make_wintitle (buf ,xsize ,ysize ,title ,act) ;	
	return;
}
//
void make_wintitle (unsigned char *buf, int xsize, int ysize, char *title ,char act) { 
	static char closebtn[14][16] = {
		"OOOOOOOOOOOOOOO@",
		"OQQQQQQQQQQQQQ$@",
		"OQQQQQQQQQQQQQ$@",
		"OQQQ@@QQQQ@@QQ$@",
		"OQQQQ@@QQ@@QQQ$@",
		"OQQQQQ@@@@QQQQ$@",
		"OQQQQQQ@@QQQQQ$@",
		"OQQQQQ@@@@QQQQ$@",
		"OQQQQ@@QQ@@QQQ$@",
		"OQQQ@@QQQQ@@QQ$@",
		"OQQQQQQQQQQQQQ$@",
		"OQQQQQQQQQQQQQ$@",
		"O$$$$$$$$$$$$$$@",
		"@@@@@@@@@@@@@@@@"
	};
	
	int x, y;
	int tc ,tbc ;//title_color ,title_back_color 
	char c;
	
	if (act !=0) { 
		tc =COL8_FFFFFF;
		tbc=COL8_000084 ;
	}else { 
		tc =COL8_C6C6C6 ;
		tbc=COL8_848484 ;
	}
	boxfill8(buf, xsize, tbc, 3,         3,         xsize - 4, 20       );
	putchars8(buf, xsize, 24, 4, tc, title);
	for (y = 0; y < 14; y++) {
		for (x = 0; x < 16; x++) {
			c = closebtn[y][x];
			if (c == '@') {
				c = COL8_000000;
			} else if (c == '$') {
				c = COL8_848484;
			} else if (c == 'Q') {
				c = COL8_C6C6C6;
			} else {
				c = COL8_FFFFFF;
			}
			buf[(5 + y) * xsize + (xsize - 21 + x)] = c;
		}
	}
	
}
void make_textbox8( SHEET *sht, int x0, int y0, int sx, int sy, int c)
{
	int x1 = x0 + sx, y1 = y0 + sy;
	boxfill8(sht->buf, sht->bxsize, COL8_848484, x0 - 2, y0 - 3, x1 + 1, y0 - 3);
	boxfill8(sht->buf, sht->bxsize, COL8_848484, x0 - 3, y0 - 3, x0 - 3, y1 + 1);
	boxfill8(sht->buf, sht->bxsize, COL8_FFFFFF, x0 - 3, y1 + 2, x1 + 1, y1 + 2);
	boxfill8(sht->buf, sht->bxsize, COL8_FFFFFF, x1 + 2, y0 - 3, x1 + 2, y1 + 2);
	boxfill8(sht->buf, sht->bxsize, COL8_000000, x0 - 1, y0 - 2, x1 + 0, y0 - 2);
	boxfill8(sht->buf, sht->bxsize, COL8_000000, x0 - 2, y0 - 2, x0 - 2, y1 + 0);
	boxfill8(sht->buf, sht->bxsize, COL8_C6C6C6, x0 - 2, y1 + 1, x1 + 0, y1 + 1);
	boxfill8(sht->buf, sht->bxsize, COL8_C6C6C6, x1 + 1, y0 - 2, x1 + 1, y1 + 1);
	boxfill8(sht->buf, sht->bxsize, c,           x0 - 1, y0 - 1, x1 + 0, y1 + 0);
	return;
}  